<template>
    <div>
        <Button  size="default"   @click="btn_refresh" type="primary" style="margin:5px">刷新</Button> 
        <i-table size="default"   border stripe :columns="format" :data="show_list" :height="sharedState.tableHeight" >
            <template slot-scope="{ row, index }" slot="action">
                <Button  size="default"   type="primary"  style="margin-right: 5px "  @click="get_bucket_object(index)">查看对象</Button>
                <Button  size="default"   type="error"  style="margin-right: 5px "  @click="del_bucket(index)">删除bucket</Button>
            </template>
        </i-table >
        <div style="text-align: center;margin-top: 10px;">
            <Page ref="page" :total="total" :page-size="pageSize"  @on-change="changePage" show-total/>
        </div>
    </div>
</template>

<script>

import { post } from  '@/api/http'
import store from '@/store'
import { get_all_buckets,delete_bucket } from  '@/api'


export default {
    data() {
        return {
            sharedState: store.state,
            format: [
                {
                    title: '名字',key: 'name',
                },
                {
                    title: '创建时间',key: 'created',
                },
                {
                    title: '操作',slot: 'action',width: 300,align: 'center'
                }
            ],
            total_list: [],
            show_list: [],
            // 分页
            total: 0,
            pageSize: 15,
        }
    },
    methods: {
        btn_refresh(){
            this.refresh()
        },
        del_bucket(index){
            let name = this.show_list[index].name
            let result = confirm("确定要删除"+name+"吗?")
            if(result == false) return 
            let data = {"bucket_name":name}
            post(delete_bucket,data,{},3000).then( (response) => {
                let info = JSON.stringify(response.data)
                if(info.indexOf('ok') != -1) {
                    this.$Message.success('删除bucket成功')
                    this.refresh()
                }else {
                    alert(info)
                }
            })
        },
        get_bucket_object(index){
            this.$router.push({name: 'bucketObjectList',query: {name: this.show_list[index].name}})
        },
        changePage(index) {
            let _start = (index -1) * this.pageSize
            let _end = index * this.pageSize
            this.show_list = this.total_list.slice(_start,_end)
        },
        refresh() {
            post(get_all_buckets,{},{},3000).then( (response) => {
                if(response.status==200){
                    this.total_list = response.data
                    this.total = response.data.length
                    if(this.total < this.pageSize) {
                        this.show_list = this.total_list
                    }else {
                        let currentPage = this.$refs.page.currentPage
                        let _start = (currentPage-1) * this.pageSize
                        let _end = currentPage * this.pageSize
                        this.show_list = this.total_list.slice(_start,_end)
                    }
                }
            }).catch(error =>{
                console.log(error)
            })
        }
    },
    mounted: function() {
        setTimeout(() =>{
            this.refresh();
        },200); 
    }
}
</script>
<style scoped>

</style>